<?php
namespace app\index\controller;
//use think\Controller;//引入这个命名空间下的类
use app\index\controller\Base;

class Article extends Base
{
    public function index()
    {
    	$arid = input('arid');
    	$articles = db('article')->find($arid);
    	$ralateres = $this->ralat($articles['keywords'],$articles['id']);//执行函数ralat()
    	db('article')->where('id','=',$arid)->setInc('click');//点击数自增
    	$cates = db('cate')->find($articles['cateid']);
    	$recres = db('article')->where(array('cateid'=>$cates['id'],'state'=>1))->limit(8)->order('id desc')->select();
    	$this->assign(array(
    		'articles'=>$articles,
    		'cates'=>$cates,
    		'recres'=>$recres,
    		'ralateres'=>$ralateres

    	));
        return $this->fetch('article');
    }

    public function ralat($keywords,$id)
    {
    	$arr = explode(',',$keywords);//以逗号不界拆分为数组
    	static $ralateres = array();//这个数组为存查出来的数据
    	foreach ($arr as $k => $v) {
    		$map['keywords'] = ['like','%'.$v.'%'];//模糊查keywords字段相似的
    		$map['id'] =['neq',$id];//把自己排除，不能等于自己的id
    		$artres = db('article')->where($map)->order('id desc')->limit(8)->select();
    		$ralateres = array_merge($ralateres,$artres);//每查到一条就合并到ralateres存起来
    	}
    	if ($ralateres) {//如果有内容
    		$ralateres = arr_unique($ralateres);//这是函数文件里自定义的函数，二维数组取重处理。
    	return $ralateres;
    	}
    	
    }
}